

package  com.bei_en.teacher.management.controller.users;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.users.condition.UsersCondition;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.service.users.UsersService;
import com.bei_en.teacher.management.controller.BaseController;
import com.bei_en.teacher.management.web.MyJsonFailResult;
import com.bei_en.teacher.management.web.MyJsonResult;
import com.bei_en.teacher.management.web.MyJsonSuccessResult;

/**
 * 账户
 * @author Guocg
 * @date 2016-7-26 22:44:04
 */
@Controller
@RequestMapping("/admin")
public class UsersController extends BaseController {

    @Autowired
    UsersService usersService;

    private void addSharedAttr(Model model) {
//	sysCodeService.findSysCodePage(vo1);
    	
    }

    @RequestMapping("/users")
    public String users(Model model,UsersCondition vo, HttpServletResponse resp ) {

		List<UsersVo> list =new ArrayList<UsersVo>();
		try {
		    list = usersService.findUsersPage(vo);
		} catch (Exception e) {
		    e.printStackTrace();
		    writeJson(resp, new MyJsonFailResult(e.getMessage()));
		    return null;
		}
	
		model.addAttribute("list", list);
		addSharedAttr(model);
		model.addAttribute("vo", vo);
	
		return "/admin/users/usersList.html";
    }

    @RequestMapping("/usersAdd")
    public String usersAdd(Model model, UsersVo vo, boolean show, HttpSession session,
	    HttpServletResponse resp) {

	if (show) {
	    addSharedAttr(model);
	    return "/admin/users/usersAdd.html";
	}
	try {
	   /* SysUserVo obj = getLoginUser(session);
	    if (obj!=null)
		vo.setCreateId(obj.getId());
	    else
		vo.setCreateId(0);*/
	    vo.setCreateTime(new Date());
	usersService.insert(vo);
	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}
	MyJsonSuccessResult re = new MyJsonSuccessResult();
	re.setCloseCurrent(true);
	re.setTabid("usersList");
	writeJson(resp, re);
	return null;
    }

    @RequestMapping("/usersEdit")
    public String usersEdit(Model model,boolean show, UsersVo vo, HttpSession session, HttpServletResponse resp) {
	
		if (show) {
		    UsersVo vo1 = usersService.findById(vo.getId());
		    addSharedAttr(model);
		    model.addAttribute("vo", vo1);
		    return "/admin/users/usersEdit.html";
		}
		try {
		   /* SysUserVo obj = getLoginUser(session);
		    if (obj!=null)
			vo.setUpdateId(obj.getId());
		    else
			vo.setUpdateId(0);*/
		    vo.setUpdateTime(new Date());
		usersService.updateSelective(vo);
		} catch (Exception e) {
		    e.printStackTrace();
		    writeJson(resp, new MyJsonFailResult(e.getMessage()));
		    return null;
		}
		MyJsonSuccessResult re = new MyJsonSuccessResult();
		re.setCloseCurrent(true);
		re.setTabid("usersList");
		writeJson(resp, re);
		return null;
    }

    @RequestMapping("/usersDel")
    @ResponseBody
    public MyJsonResult usersDel(Model model, int[] delids, HttpSession session) {

		try {
		    for (int i : delids) {
			UsersVo vo1 = new UsersVo();
			vo1.setId(i);
//			vo1.setStatus(0);
			/*SysUserVo obj = getLoginUser(session);
			if (obj!=null)
			    vo1.setUpdateId(obj.getId());
			else
			    vo1.setUpdateId(0);*/
			vo1.setUpdateTime(new Date());
			usersService.updateSelective(vo1);
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		    return new MyJsonFailResult(e.getMessage());
		}
		return new MyJsonSuccessResult();
	}
    
}
